package org.springframework.samples.webflow.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.webflow.dao.CommonDao;
import org.springframework.samples.webflow.dao.CommonDaoImpl;
import org.springframework.samples.webflow.dao.Criteria;
import org.springframework.samples.webflow.dao.Criteria.Operator;
import org.springframework.samples.webflow.dao.SearchCriteria;
import org.springframework.samples.webflow.entity.Category;
import org.springframework.samples.webflow.entity.Customer;
import org.springframework.samples.webflow.entity.Product;
import org.springframework.stereotype.Controller;

@Controller
public class CustomerController {

	@Autowired
	private CommonDao commonService;
	
    private String customerId;
    
    private String taxId;


    private String name;


    private String adresse;


    private String city;


    private String state;


    private String zip;


    private String phone;
    
    private List<Customer> customers;
    
    private List<SelectItem> categories;
    
    private List<SelectItem> products;
    
    private List<Product> productList;
    
    private String categoryId;
    
    private String productId;
    
    private Customer customer;

    public void initSearchCriteria(SearchCriteria searchCriteria) {
    	Criteria criteria = new Criteria(Operator.LIKE, null); 
    	searchCriteria.addCriteria("name", criteria);
    }
    
    private void loadCategories() {
    	categories = new ArrayList<SelectItem>();
    	Collection<Category> list = commonService.getAllCategories();
    	for (Category item : list) {
    		categories.add(new SelectItem(item.getCategoryId(), item.getName()));
    	}
    }
    
    public void initAddProducts() {
    	loadCategories();
    	productList = new ArrayList<Product>();
    	products = new ArrayList<SelectItem>();
    }    
    
    public String initCreate() {
    	taxId = null;
	    name = null;
	    adresse = null;
	    city = null;
	    state = null;
	    zip = null;
	    phone = null;
	    return "success";
    }
    
    public void categoryChanged(ValueChangeEvent event) {
    	products = new ArrayList<SelectItem>();
    	if (event.getNewValue() != null) {
    		ArrayList<Product> lista = (ArrayList<Product>)commonService.getProductsByCategory(Long.parseLong((String)event.getNewValue()));
	    	for (Product product : lista) {
	    		SelectItem item = new SelectItem(product.getProductId(), product.getName());
	    		products.add(item);
	    	}
    	}
    }
    
    public void addProduct() {
    	if (productId == null || productId.equals("")) {
    		FacesContext facesContext = FacesContext.getCurrentInstance();
    		facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Must Select Product", "Must Select Product"));
    		return;
    	}
    	
    	Product product = commonService.getProductById(Long.parseLong(productId));
    	if (productExist(product)) {
    		FacesContext facesContext = FacesContext.getCurrentInstance();
    		facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Product Exist in the list", "Product Exist in the list"));
    	} else {
    		productList.add(product);
    	}
    }
    
    public boolean customerExist(Customer customer) {
    	Collection<Customer> list =  commonService.getAll();
    	for (Customer item : list) {
    		if (item.getTaxId().equals(customer.getTaxId())) return true;
    	}
    	return false;
    }
    
    public boolean productExist(Product product) {
    	for (Product item : productList) {
    		if (item.getProductId().equals(product.getProductId())) return true;
    	}
    	return false;
    }
    
    public String save(){
    	customer= new Customer(Integer.parseInt(taxId), name, adresse, city, state, zip, phone);
    	if (customerExist(customer)) {
    		FacesContext facesContext = FacesContext.getCurrentInstance();
    		facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Costumer with Taxid: " + taxId + " already exists", "Costumer with Taxid " + taxId + " already exists"));
    		return null;
    	} else {
    		commonService.save(customer);
	    	productList = new ArrayList<Product>();
	    	products = new ArrayList<SelectItem>();
	    	loadCategories();
	    	categoryId = null;
	    	return "success";
    	}
    }

    public String select() {
    	FacesContext facesContext = FacesContext.getCurrentInstance();
    	Map<String, String> parameterMap = (Map<String, String>) facesContext.getExternalContext().getRequestParameterMap();
    	String clientId = parameterMap.get("clientId");
    	customer = commonService.getById(Long.parseLong(clientId));
    	productList = new ArrayList<Product>();
    	products = new ArrayList<SelectItem>();
    	loadCategories();
    	categoryId = null;
    	return "select";
    }

	public void setCommonService(CommonDaoImpl commonService) {
		this.commonService = commonService;
	}

	public String getCustomerId() {
		return customerId;
	}


	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}


	public String getTaxId() {
		return taxId;
	}


	public void setTaxId(String taxId) {
		this.taxId = taxId;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getAdresse() {
		return adresse;
	}


	public void setAdresse(String adresse) {
		this.adresse = adresse;
	}


	public String getCity() {
		return city;
	}


	public void setCity(String city) {
		this.city = city;
	}


	public String getState() {
		return state;
	}


	public void setState(String state) {
		this.state = state;
	}


	public String getZip() {
		return zip;
	}


	public void setZip(String zip) {
		this.zip = zip;
	}


	public String getPhone() {
		return phone;
	}


	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public void search() {
		customers = (List<Customer>) commonService.findByName(name);
		System.out.println("customers.size = " + customers.size());
	}

	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public List<SelectItem> getCategories() {
		return categories;
	}

	public List<SelectItem> getProducts() {
		return products;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public List<Product> getProductList() {
		return productList;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}
}
